﻿using Sidvall.Serialization;
using Sidvall.UI.Messaging;

namespace Sidvall.UI.ViewModels
{
    public abstract class MainWindowViewModelBase : ControlViewModelBase
    {
        #region Public Members

        #region ApplicationStatus

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public ApplicationStatus ApplicationStatus
        {
            get
            {
                if (Sidvall.UI.SystemContext.Current.ApplicationManager == null)
                    return UI.ApplicationStatus.Initializing;
                return Sidvall.UI.SystemContext.Current.ApplicationManager.Status;
            }
        }

        #endregion
        #region ApplicationContext

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public string ApplicationContext
        {
            get
            {
                if (Sidvall.UI.SystemContext.Current.ApplicationManager == null)
                    return null;
                return Sidvall.UI.SystemContext.Current.ApplicationManager.Context;
            }
        }

        #endregion
        #region ApplicationSetupCompleted

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public bool ApplicationSetupCompleted
        {
            get
            {
                if (Sidvall.UI.SystemContext.Current.ApplicationManager == null)
                    return false;
                return Sidvall.UI.SystemContext.Current.ApplicationManager.SetupCompleted;
            }
        }

        #endregion
        #region IsBusyAndStarted

        public bool IsBusyAndStarted
        {
            get
            {
                if (!this.IsBusy)
                    return false;
                if (this.ApplicationStatus != Sidvall.UI.ApplicationStatus.Started)
                    return false;
                return true;
            }
        }

        #endregion
        #region IsNotBusy

        public bool IsNotBusy
        {
            get
            {
                if (this.IsBusy)
                    return false;
                return true;
            }
        }

        #endregion
        #region IsNotBusyAndStarted

        public bool IsNotBusyAndStarted
        {
            get
            {
                if (this.IsBusy)
                    return false;
                if (this.ApplicationStatus != Sidvall.UI.ApplicationStatus.Started)
                    return false;
                return true;
            }
        }

        #endregion

        #region OnApplicationMessage

        protected virtual void OnApplicationMessage()
        {
        }

        #endregion
        #region OnIsBusyChanged

        protected override void OnIsBusyChanged(bool isBusy, string message, string scope)
        {
            RaisePropertyChanged(nameof(IsBusyAndStarted));
            RaisePropertyChanged(nameof(IsNotBusy));
            RaisePropertyChanged(nameof(IsNotBusyAndStarted));
            OnIsBusyChanged();
            OnIsBusyChanged(this, new BusyChangedEventArgs(this.IsBusy, message, scope));
        }

        #endregion
        #region OnViewModelControlsIsBusyChanged

        protected override void OnViewModelControlsIsBusyChanged(object sender, BusyChangedEventArgs e)
        {
            SetIsBusy(e.IsBusy, e.Message, e.Scope);
        }

        #endregion
        #region Start

        public virtual void Start()
        {
            Subscribe<ApplicationMessage, ViewModelMessage>(OnApplicationMessage, OnViewModelMessage);
        }

        #endregion

        #endregion
        #region Private Members

        #region OnApplicationMessage

        private void OnApplicationMessage(ApplicationMessage item)
        {
            RaisePropertyChanged(nameof(ApplicationStatus));
            RaisePropertyChanged(nameof(ApplicationContext));
            RaisePropertyChanged(nameof(ApplicationSetupCompleted));
            RaisePropertyChanged(nameof(IsBusyAndStarted));
            RaisePropertyChanged(nameof(IsNotBusy));
            RaisePropertyChanged(nameof(IsNotBusyAndStarted));
            OnApplicationMessage();
        }

        #endregion
        #region OnViewModelMessage

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Sidvall.Serialization.StringExtension.Equals(System.String,System.String,System.Boolean,System.StringComparison)")]
        private void OnViewModelMessage(ViewModelMessage item)
        {
            if (ViewModelMessage.IsBusyMessage(item))
            {
                var isBusy = item.Value.ToBoolean(false);
                var context = BusyContext.Deserialize(item.Context);
                SetIsBusy(isBusy, context.Message, context.Scope);
            }
        }

        #endregion

        #endregion
        #region Constructors

        protected MainWindowViewModelBase()
        {
        }

        #endregion
    }
}
